package com.commerce.user.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.commerce.user.utils.AliOssUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
@ConfigurationProperties(prefix = "alioss")
@Slf4j
public class AliOSSConfig {
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;

    /**
     * 创建OSS客户端
     */
    @Bean
    public OSS ossClient() {
        if (endpoint == null || endpoint.isEmpty() || accessKeyId == null || accessKeyId.isEmpty() || accessKeySecret == null || accessKeySecret.isEmpty()) {
            log.warn("阿里云OSS配置不完整，无法创建OSS客户端");
            return null;
        }
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }
    
    /**
     * 创建阿里云文件上传工具类对象
     */
    @Bean
    public AliOssUtil aliOssUtil() {
        log.info("创建阿里云文件上传工具类对象：{}", this);
        return new AliOssUtil(endpoint, accessKeyId, accessKeySecret, bucketName);
    }
} 